// @algorithm @lc id=100311 lang=cpp
// @title cong-shang-dao-xia-da-yin-er-cha-shu-lcof

#include "algm/algm.h"
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> levelOrder(TreeNode *root) {
        if (root == nullptr) {
            return {};
        }
        vector<int> ans;
        queue<TreeNode *> q;
        q.push(root);
        while (!q.empty()) {
            int cnt = q.size();
            while (cnt--) {
                TreeNode *temp = q.front();
                q.pop();
                ans.push_back(temp->val);
                if (temp->left) {
                    q.push(temp->left);
                }
                if (temp->right) {
                    q.push(temp->right);
                }
            }
        }
        return ans;
    }
};